﻿// Decompiled with JetBrains decompiler
// Type: Microsoft.InfoCards.SelfIssuedAuthProofToken
// Assembly: infocard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 8E14765A-6610-409A-BA36-099A0642905D
// Assembly location: E:\git\ALLIDA\windll\infocard.exe

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IdentityModel.Tokens;
using System.Security.Cryptography;

namespace Microsoft.InfoCards
{
  internal class SelfIssuedAuthProofToken : SecurityToken, IDisposable
  {
    private string m_id;
    private DateTime m_expiration;
    private ReadOnlyCollection<SecurityKey> m_securityKeys;
    private SecurityKey m_securityKey;
    private bool m_isSymmetric;
    private RSACryptoServiceProvider m_publicKey;

    public SelfIssuedAuthProofToken(RSACryptoServiceProvider rsa, DateTime expiration)
      : this(expiration)
    {
      this.m_publicKey = new RSACryptoServiceProvider();
      this.m_publicKey.ImportCspBlob(rsa.ExportCspBlob(false));
      this.InitCrypto((SecurityKey) new SelfIssuedAuthAsymmetricKey(rsa));
    }

    public SelfIssuedAuthProofToken(InMemorySymmetricSecurityKey symKey, DateTime expiration)
      : this(expiration)
    {
      this.m_isSymmetric = true;
      this.InitCrypto((SecurityKey) symKey);
    }

    private SelfIssuedAuthProofToken(DateTime expiration)
    {
      this.m_id = Guid.NewGuid().ToString();
      this.m_expiration = expiration.ToUniversalTime();
    }

    public override string Id
    {
      get
      {
        return this.m_id;
      }
    }

    public override ReadOnlyCollection<SecurityKey> SecurityKeys
    {
      get
      {
        return this.m_securityKeys;
      }
    }

    public override DateTime ValidTo
    {
      get
      {
        return this.m_expiration;
      }
    }

    public override DateTime ValidFrom
    {
      get
      {
        return DateTime.UtcNow;
      }
    }

    private void InitCrypto(SecurityKey securityKey)
    {
      this.m_securityKey = securityKey;
      this.m_securityKeys = new List<SecurityKey>(1)
      {
        securityKey
      }.AsReadOnly();
    }

    public override bool CanCreateKeyIdentifierClause<T>()
    {
      return typeof (T) == typeof (RsaKeyIdentifierClause);
    }

    public override T CreateKeyIdentifierClause<T>()
    {
      if (typeof (T) == typeof (RsaKeyIdentifierClause))
        return (T) new RsaKeyIdentifierClause((RSA) this.m_publicKey);
      return base.CreateKeyIdentifierClause<T>();
    }

    public override bool MatchesKeyIdentifierClause(SecurityKeyIdentifierClause keyIdentifierClause)
    {
      RsaKeyIdentifierClause identifierClause = keyIdentifierClause as RsaKeyIdentifierClause;
      if (identifierClause != null)
        return identifierClause.Matches((RSA) this.m_publicKey);
      return false;
    }

    public void Dispose()
    {
      if (this.m_securityKey != null && !this.m_isSymmetric)
      {
        ((IDisposable) this.m_securityKey).Dispose();
        this.m_securityKey = (SecurityKey) null;
        this.m_securityKeys = (ReadOnlyCollection<SecurityKey>) null;
      }
      if (this.m_publicKey == null)
        return;
      ((IDisposable) this.m_publicKey).Dispose();
      this.m_publicKey = (RSACryptoServiceProvider) null;
    }
  }
}
